package com.eduadmin.controller;

import com.eduadmin.entity.*;
import com.eduadmin.service.*;
import com.eduadmin.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class QueryTeachScoreController {
    private final IScoreService scoreService;
    private final ITeachInfoService teachInfoService;
    private final ITeacherService teacherService;
    private final IGradeService gradeService;

    @Autowired
    public QueryTeachScoreController(
            IScoreService scoreService,
            ITeacherService teacherService,
            ITeachInfoService teachInfoService,
            IGradeService gradeService) {
        this.scoreService = scoreService;
        this.teacherService = teacherService;
        this.teachInfoService = teachInfoService;
        this.gradeService = gradeService;
    }

   // zlc： 教师登录成功后，查询自己的教学成绩
   @RequestMapping(value = "queryMyTeachScore")
   @ResponseBody
   public Map<String,Object> queryMyTeachScore(
           @RequestParam(value = "page", required = false) String page,
           @RequestParam(value = "rows", required = false) String rows,
           @RequestParam(value = "gradeName", required = false, defaultValue = "") String gradeName,
           @RequestParam(value = "gradeDatatableName", required = false, defaultValue = "t_score") String gradeDatatableName, //空表 避免首次进入报错
           @RequestParam(value = "teamName", required = false, defaultValue = "") String teamName,
           @RequestParam(value = "semesterName", required = false, defaultValue = "") String semesterName,
           @RequestParam(value = "examName", required = false, defaultValue = "") String examName,
           @RequestParam(value = "courseEnName", required = false, defaultValue = "chinese") String courseEnName,
           HttpServletRequest request
   ){
        Map<String,Object> paraMap=new HashMap<>();
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());
        paraMap.put("gradeName",gradeName);
        paraMap.put("gradeDatatableName",gradeDatatableName);
        paraMap.put("teamName",teamName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("courseEnName",courseEnName);
        List<TeachScore> teachScoreList=scoreService.getTeachScoreByParam(paraMap);
       Map<String,Object> reMap=new HashMap<>();
       int total = scoreService.getTeachScoreNumberByParam(paraMap);
       reMap.put("rows", teachScoreList);
       reMap.put("total", total);
       return reMap;
   }
   // zlc: 教师登录成功后，导出自己查询到的教学成绩
    @RequestMapping(value = "exportMyTeachScoreData")
    public void exportMyTeachScoreData( @RequestParam(value = "gradeName", required = false, defaultValue = "") String gradeName,
                                        @RequestParam(value = "gradeDatatableName", required = false, defaultValue = "t_score") String gradeDatatableName, //空表 避免首次进入报错
                                        @RequestParam(value = "teamName", required = false, defaultValue = "") String teamName,
                                        @RequestParam(value = "semesterName", required = false, defaultValue = "") String semesterName,
                                        @RequestParam(value = "examName", required = false, defaultValue = "") String examName,
                                        @RequestParam(value = "courseEnName", required = false, defaultValue = "chinese") String courseEnName,
                                        HttpServletResponse response){
        Map<String,Object> paraMap=new HashMap<>();
        paraMap.put("gradeName",gradeName);
        paraMap.put("gradeDatatableName",gradeDatatableName);
        paraMap.put("teamName",teamName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("courseEnName",courseEnName);
        List<TeachScore> teachScoreList=scoreService.getTeachScoreForExportByParam(paraMap);
        HashMap<String,String> courseMap=new HashMap<String,String>(){{
            put("chinese","语文");
            put("maths","数学");
            put("english","英语");
            put("physics","物理");
            put("history","历史");
            put("chemistry","化学");
            put("biology","生物");
            put("politics","政治");
            put("geography","地理");
            put("sports","体育");
        }};
        String courseChName=courseMap.get(courseEnName);
        for (int i=0;i<teachScoreList.size();i++)
            teachScoreList.get(i).setSubjectName(courseChName);// 存储任教科目字段值

        try {
            ExcelUtils.export2Web(response, teamName+semesterName+examName+courseChName+"成绩表", teamName+semesterName+examName+courseChName+"成绩表", TeachScore.class, teachScoreList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}